import {createRouter,createWebHashHistory} from 'vue-router'
import Home from '../views/Home.vue'
import Category from '../views/Category.vue'
import Cart from '../views/Cart.vue'
// import VuexTest from '../views/VuexTest/index.vue'
import Detail from '../views/Detail.vue'
import NotLogin from '../views/NotLogin.vue'
import Login from '../views/Login.vue'
import Reg from '../views/Reg.vue'
import Main from '../views/Main.vue'
import Mine from '../views/Mine.vue'
import AddressList from '../views/Address/List.vue'
import AddressDetail from '../views/Address/Detail.vue'
import store from '../store/index.js'
const router = createRouter({ //定义路由对象
  history:createWebHashHistory(),
  routes:[
    {
      path:'/',
      component:Main,
      children:[
        {
          path:'home',
          alias:'/',
          component:Home
        },
        {
          path:'category',
          component:Category
        },
        {
          path:'cart',
          component:Cart
        },
        {
          path:'mine',
          component:Mine
        },
        {
          path:'/notlogin',
          component:NotLogin
        },
      ]
    },
    {
      path:'/detail',
      component:Detail
    },
    {
      path:'/address/list',
      component:AddressList
    },
    {
      path:'/address/detail',
      component:AddressDetail
    },
    
    {
      path:'/login',
      component:Login
    },
    {
      path:'/reg',
      component:Reg
    },
    {
      path:'/vuex/test',
      component:()=>import('../views/VuexTest/index.vue')
    }
  ]
})

router.beforeEach((to,from)=>{  //全局守卫
  console.log(to,from);
  if(to.path=='/cart'){
   if(!store.state.user.userInfo){
      return '/notlogin'
   }
   return true  //放行
  }
  return true
})

export default router